package com.example.demo.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherUtils {
    /**
     * 手机字段正则表达式
     **/
    public static final String MOBILE_PATTERN = "^\\d{11}$";
//	public static final String MOBILE_PATTERN = "^((13[0-9])|(14[5,7])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
    /**
     * 固定电话正则验证
     **/
    public static final String TEl_PATTERN = "^(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{8}$";

    /**
     * 微信
     **/
    public static final String WECHAT_PATTERN = "^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$";

    /**
     * 邮箱字段正则表达式
     **/
    public static final String EMAIL_PATTERN = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    /**
     * 登录名
     **/
    public static final String LOGIN_NAME_PATTERN = "^\\w{5,12}$";
    /**
     * int
     **/
    public static final String INT_PATTERN = "^[0-9_]+$";

    /**
     * 正则表达式匹配
     *
     * @param input输入要匹配的值
     * @param regex正则表达式
     * @return
     */
    public static boolean isMatcher(String input, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        return matcher.find();
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isInt(String str) {
        return str.matches(INT_PATTERN);
    }

    /**
     * 验证email格式
     *
     * @param str
     * @return
     */
    public static boolean isEmail(String str) {
        return isMatcher(str, EMAIL_PATTERN);
    }

    /**
     * 验证手机号码格式
     *
     * @param str
     * @return
     */
    public static boolean isMobile(String str) {
        return isMatcher(str, MOBILE_PATTERN);
    }

    /**
     * 验证固话
     *
     * @param str
     * @return
     */
    public static boolean isTel(String str) {
        return isMatcher(str, TEl_PATTERN);
    }

    public static void main(String[] args) {
        System.out.println(isTel("0345-55234876"));
        System.out.println(isMobile("18112997351"));
    }

}
